package com.tenton.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenton.MyException.DataNotExist;
import com.tenton.mapper.CommodityTypeMapper;
import com.tenton.pojo.CommodityType;
import com.tenton.service.CommodityTypeService;
import com.tenton.utils.Constants;
import com.tenton.utils.IntegerUtils;
import com.tenton.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@Service
public class CommodityTypeServiceImpl implements CommodityTypeService {
    @Autowired
    CommodityTypeMapper commodityTypeMapper;

    /**
     * 删除商品类型
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("商品类型Id为空");
        } else {
            return commodityTypeMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * 新增商品类型
     *
     * @param commodityType
     * @param session
     * @return
     */
    @Override
    public int insert(CommodityType commodityType, HttpSession session) throws DataNotExist {
        if (commodityType == null){
            throw new DataNotExist("商品类型数据为空");
        }else {
            String name = (String) session.getAttribute("name");
            commodityType.setCreateName(name);
            Date date = new Date();
            commodityType.setCreateTime(date);
            return commodityTypeMapper.insert(commodityType);
        }
    }

    /**
     * 获取所有商品类型信息
     *
     * @param model
     * @param pn
     * @return
     */
    @Override
    public void getAll(ModelMap model, Integer pn) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<CommodityType> types = commodityTypeMapper.selectAll();
        PageInfo<CommodityType> pageInfo = new PageInfo<CommodityType>(types);
        model.addAttribute("pageInfo", pageInfo);
    }

    /**
     * 查询商品类型
     *
     * @param id
     * @param model
     * @return
     */
    @Override
    public void selectByPrimaryKey(Integer id, Model model) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("商品Id为空");
        } else {
            model.addAttribute("commodityType", commodityTypeMapper.selectByPrimaryKey(id));
        }
    }

    /**
     * 查询商品类型
     *
     * @param comType
     * @return
     */
    @Override
    public CommodityType selectByName(String comType) {
        if (StringUtils.isEmpty(comType)) {
            throw new NullPointerException("商品名称为空");
        } else {
            return commodityTypeMapper.selectByName(comType);
        }
    }

    /**
     * 修改商品类型
     *
     * @param commodityType
     * @param session
     * @return
     */
    @Override
    public boolean update(CommodityType commodityType,HttpSession session) throws DataNotExist {
        if (commodityType != null) {
            String name = (String) session.getAttribute("name");
            commodityType.setUpdateName(name);
            Date date = new Date();
            commodityType.setUpdateTime(date);
            return commodityTypeMapper.updateByPrimaryKey(commodityType);
        } else {
            throw new DataNotExist("商品类型为空");
        }
    }

    /**
     * 商品类型模糊查询
     *
     * @param comType
     * @param pn
     * @param model
     */
    @Override
    public void getByParams(String comType, Integer pn, ModelMap model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<CommodityType> commodityTypes = commodityTypeMapper.getByParams(comType);
        PageInfo<CommodityType> pageInfo = new PageInfo<CommodityType>(commodityTypes);
        model.addAttribute("pageInfo", pageInfo);
    }
}
